home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfcid0.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  14.9 KB  |  526 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfcid0.c,v 1.4 2000/09/19 19:00:53 lpd Exp $ */
  20. /* CIDFontType 0 operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"
  25. #include "gsccode.h"
  26. #include "gsstruct.h"
  27. #include "gxfcid.h"
  28. #include "gxfont1.h"
  29. #include "stream.h"        /* for files.h */
  30. #include "bfont.h"
  31. #include "files.h"
  32. #include "ichar.h"
  33. #include "ichar1.h"
  34. #include "icid.h"
  35. #include "idict.h"
  36. #include "idparam.h"
  37. #include "ifcid.h"
  38. #include "ifont1.h"
  39. #include "ifont2.h"
  40. #include "ifont42.h"
  41. #include "store.h"
  42.  
  43. /* Type 1 font procedures (defined in zchar1.c) */
  44. font_proc_glyph_outline(zcharstring_glyph_outline);
  45.  
  46. /* ---------------- CIDFontType 0 (FontType 9) ---------------- */
  47.  
  48. /* ------ Accessing ------ */
  49.  
  50. /* Parse a multi-byte integer from a string. */
  51. private int
  52. get_index(gs_const_string *pgstr, int count, ulong *pval)
  53. {
  54.     int i;
  55.  
  56.     if (pgstr->size < count)
  57.     return_error(e_rangecheck);
  58.     *pval = 0;
  59.     for (i = 0; i < count; ++i)
  60.     *pval = (*pval << 8) + pgstr->data[i];
  61.     pgstr->data += count;
  62.     pgstr->size -= count;
  63.     return 0;
  64. }
  65.  
  66. /* Get bytes from GlyphData or DataSource. */
  67. private int
  68. cid0_read_bytes(gs_font_cid0 *pfont, ulong base, uint count, byte *buf,
  69.         gs_const_string *pstr)
  70. {
  71.     const font_data *pfdata = pfont_data(pfont);
  72.     byte *data = buf;
  73.     int code = 0;
  74.  
  75.     /* Check for overflow. */
  76.     if (base != (long)base || base > base + count)
  77.     return_error(e_rangecheck);
  78.     if (r_has_type(&pfdata->u.cid0.DataSource, t_null)) {
  79.     /* Get the bytes from GlyphData (a string or array of strings). */
  80.     const ref *pgdata = &pfdata->u.cid0.GlyphData;
  81.  
  82.     if (r_has_type(pgdata, t_string)) {  /* single string */
  83.         uint size = r_size(pgdata);
  84.  
  85.         if (base >= size || count > size - base)
  86.         return_error(e_rangecheck);
  87.         data = pgdata->value.bytes + base;
  88.     } else {        /* array of strings */
  89.         /*
  90.          * The algorithm is similar to the one in
  91.          * string_array_access_proc in zfont42.c, but it also has to
  92.          * deal with the case where the requested string crosses array
  93.          * elements.
  94.          */
  95.         ulong skip = base;
  96.         uint copied = 0;
  97.         uint index = 0;
  98.         ref rstr;
  99.         uint size;
  100.  
  101.         for (;; skip -= size, ++index) {
  102.         int code = array_get(pgdata, index, &rstr);
  103.  
  104.         if (code < 0)
  105.             return code;
  106.         if (!r_has_type(&rstr, t_string))
  107.             return_error(e_typecheck);
  108.         size = r_size(&rstr);
  109.         if (skip < size)
  110.             break;
  111.         }
  112.         size -= skip;
  113.         if (count <= size) {
  114.         data = rstr.value.bytes + skip;
  115.         } else {        /* multiple strings needed */
  116.         if (data == 0) {  /* no buffer provided */
  117.             data = gs_alloc_string(pfont->memory, count,
  118.                        "cid0_read_bytes");
  119.             if (data == 0)
  120.             return_error(e_VMerror);
  121.             code = 1;
  122.         }
  123.         memcpy(data, rstr.value.bytes + skip, size);
  124.         copied = size;
  125.         while (copied < count) {
  126.             int code = array_get(pgdata, ++index, &rstr);
  127.  
  128.             if (code < 0)
  129.             goto err;
  130.             if (!r_has_type(&rstr, t_string)) {
  131.             code = gs_note_error(e_typecheck);
  132.             goto err;
  133.             }
  134.             size = r_size(&rstr);
  135.             if (size > count - copied)
  136.             size = count - copied;
  137.             memcpy(data + copied, rstr.value.bytes, size);
  138.             copied += size;
  139.         }
  140.         }
  141.     }
  142.     } else {
  143.     /* Get the bytes from DataSource (a stream). */
  144.     stream *s;
  145.     uint nread;
  146.  
  147.     check_read_known_file(s, &pfdata->u.cid0.DataSource, return_error);
  148.     if (sseek(s, base) < 0)
  149.         return_error(e_ioerror);
  150.     if (data == 0) {    /* no buffer provided */
  151.         data = gs_alloc_string(pfont->memory, count, "cid0_read_bytes");
  152.         if (data == 0)
  153.         return_error(e_VMerror);
  154.         code = 1;
  155.     }
  156.     if (sgets(s, data, count, &nread) < 0 || nread != count) {
  157.         code = gs_note_error(e_ioerror);
  158.         goto err;
  159.     }
  160.     }
  161.     pstr->data = data;
  162.     pstr->size = count;
  163.     return code;
  164.  err:
  165.     if (data != buf)
  166.     gs_free_string(pfont->memory, data, count, "cid0_read_bytes");
  167.     return code;
  168. }
  169.  
  170. /* Get the CharString data for a CIDFontType 0 font. */
  171. /* This is the glyph_data procedure in the font itself. */
  172. /* Note that pgstr may be NULL. */
  173. private int
  174. z9_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_const_string *pgstr,
  175.           int *pfidx)
  176. {
  177.     gs_font_cid0 *pfont = (gs_font_cid0 *)pbfont;
  178.     const font_data *pfdata = pfont_data(pfont);
  179.     long glyph_index = (long)(glyph - gs_min_cid_glyph);
  180.     gs_const_string gstr;
  181.     ulong fidx;
  182.     int code;
  183.  
  184.     if (!r_has_type(&pfdata->u.cid0.GlyphDirectory, t_null)) {
  185.     code = font_gdir_get_outline(&pfdata->u.cid0.GlyphDirectory,
  186.                      glyph_index, &gstr);
  187.     if (code < 0)
  188.         return code;
  189.     /* Get the definition from GlyphDirectory. */
  190.     if (gstr.data) {
  191.         code = get_index(&gstr, pfont->cidata.FDBytes, &fidx);
  192.         if (code < 0)
  193.         return code;
  194.         if (fidx >= pfont->cidata.FDArray_size)
  195.         return_error(e_rangecheck);
  196.         if (pgstr)
  197.         *pgstr = gstr;
  198.         *pfidx = (int)fidx;
  199.         return code;
  200.     }
  201.     }
  202.     /* Get the definition from the binary data (GlyphData or DataSource). */
  203.     if (glyph_index < 0 || glyph_index >= pfont->cidata.common.CIDCount) {
  204.     *pfidx = 0;
  205.     if (pgstr)
  206.         pgstr->data = 0, pgstr->size = 0;
  207.     return_error(e_undefined);
  208.     }
  209.     {
  210.     byte fd_gd[(MAX_FDBytes + MAX_GDBytes) * 2];
  211.     uint num_bytes = pfont->cidata.FDBytes + pfont->cidata.common.GDBytes;
  212.     ulong base = pfont->cidata.CIDMapOffset + glyph_index * num_bytes;
  213.     ulong gidx, gidx_next;
  214.  
  215.     code = cid0_read_bytes(pfont, base, (ulong)(num_bytes * 2), fd_gd,
  216.                    &gstr);
  217.     if (code < 0)
  218.         return code;
  219.     get_index(&gstr, pfont->cidata.FDBytes, &fidx);
  220.     get_index(&gstr, pfont->cidata.common.GDBytes, &gidx);
  221.     /*
  222.      * Some CID fonts (from Adobe!) have invalid font indexes for
  223.      * missing glyphs.  Handle this now.
  224.      */
  225.     gstr.data += pfont->cidata.FDBytes;
  226.     gstr.size -= pfont->cidata.FDBytes;
  227.     get_index(&gstr, pfont->cidata.common.GDBytes, &gidx_next);
  228.     if (gidx_next <= gidx) { /* missing glyph */
  229.         *pfidx = 0;
  230.         if (pgstr)
  231.         pgstr->data = 0, pgstr->size = 0;
  232.         return_error(e_undefined);
  233.     }
  234.     if (fidx >= pfont->cidata.FDArray_size)
  235.         return_error(e_rangecheck);
  236.     *pfidx = (int)fidx;
  237.     if (pgstr == 0)
  238.         return 0;
  239.     return cid0_read_bytes(pfont, gidx, gidx_next - gidx, NULL, pgstr);
  240.     }
  241. }
  242.  
  243. /* Get the outline of a CIDFontType 0 glyph. */
  244. private int
  245. z9_glyph_outline(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
  246.          gx_path *ppath)
  247. {
  248.     gs_font_cid0 *const pfcid = (gs_font_cid0 *)font;
  249.     ref gref;
  250.     gs_const_string gstr;
  251.     int code, fidx, ocode;
  252.  
  253.     code = pfcid->cidata.glyph_data((gs_font_base *)pfcid, glyph, &gstr,
  254.                     &fidx);
  255.     if (code < 0)
  256.     return code;
  257.     glyph_ref(glyph, &gref);
  258.     ocode = zcharstring_outline(pfcid->cidata.FDArray[fidx], &gref, &gstr,
  259.                 pmat, ppath);
  260.     if (code > 0)
  261.     gs_free_const_string(font->memory, gstr.data, gstr.size,
  262.                  "z9_glyph_outline");
  263.     return ocode;
  264. }
  265.  
  266. /*
  267.  * The "fonts" in the FDArray don't have access to their outlines -- the
  268.  * outlines are always provided externally.  Replace the accessor procedures
  269.  * with ones that will give an error if called.
  270.  */
  271. private int
  272. z9_FDArray_glyph_data(gs_font_type1 * pfont, gs_glyph glyph,
  273.               gs_const_string * pgdata)
  274. {
  275.     return_error(e_invalidfont);
  276. }
  277. private int
  278. z9_FDArray_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph,
  279.              gs_const_string *pstr)
  280. {
  281.     return_error(e_invalidfont);
  282. }
  283.  
  284. /* ------ Defining ------ */
  285.  
  286. /* Get one element of a FDArray. */
  287. private int
  288. fd_array_element(i_ctx_t *i_ctx_p, gs_font_type1 **ppfont, ref *prfd)
  289. {
  290.     charstring_font_refs_t refs;
  291.     gs_type1_data data1;
  292.     build_proc_refs build;
  293.     gs_font_base *pbfont;
  294.     gs_font_type1 *pfont;
  295.     /*
  296.      * Standard CIDFontType 0 fonts have Type 1 fonts in the FDArray, but
  297.      * CFF CIDFontType 0 fonts have Type 2 fonts there.
  298.      */
  299.     int fonttype = 1;        /* default */
  300.     int code = charstring_font_get_refs(prfd, &refs);
  301.  
  302.     if (code < 0 ||
  303.     (code = dict_int_param(prfd, "FontType", 1, 2, 1, &fonttype)) < 0
  304.     )
  305.     return code;
  306.     /*
  307.      * We don't handle the alternate Subr representation (SubrCount,
  308.      * SDBytes, SubrMapOffset) here: currently that is handled in
  309.      * PostScript code (lib/gs_cidfn.ps).
  310.      */
  311.     switch (fonttype) {
  312.     case 1:
  313.     data1.interpret = gs_type1_interpret;
  314.     data1.subroutineNumberBias = 0;
  315.     data1.lenIV = DEFAULT_LENIV_1;
  316.     code = charstring_font_params(prfd, &refs, &data1);
  317.     if (code < 0)
  318.         return code;
  319.     code = build_proc_name_refs(&build,
  320.                     "%Type1BuildChar", "%Type1BuildGlyph");
  321.     break;
  322.     case 2:
  323.     code = type2_font_params(prfd, &refs, &data1);
  324.     if (code < 0)
  325.         return code;
  326.     code = charstring_font_params(prfd, &refs, &data1);
  327.     if (code < 0)
  328.         return code;
  329.     code = build_proc_name_refs(&build,
  330.                     "%Type2BuildChar", "%Type2BuildGlyph");
  331.     break;
  332.     default:            /* can't happen */
  333.     return_error(e_Fatal);
  334.     }
  335.     if (code < 0)
  336.     return code;
  337.     code = build_gs_FDArray_font(i_ctx_p, prfd, &pbfont, fonttype,
  338.                  &st_gs_font_type1, &build);
  339.     if (code < 0)
  340.     return code;
  341.     pfont = (gs_font_type1 *)pbfont;
  342.     charstring_font_init(pfont, &refs, &data1);
  343.     pfont->data.procs.glyph_data = z9_FDArray_glyph_data;
  344.     pfont->data.procs.seac_data = z9_FDArray_seac_data;
  345.     *ppfont = pfont;
  346.     return 0;
  347. }
  348.  
  349. /* <string|name> <font_dict> .buildfont9 <string|name> <font> */
  350. gs_private_st_ptr(st_gs_font_type1_ptr, gs_font_type1 *, "gs_font_type1 *",
  351.   font1_ptr_enum_ptrs, font1_ptr_reloc_ptrs);
  352. gs_private_st_element(st_gs_font_type1_ptr_element, gs_font_type1 *,
  353.   "gs_font_type1 *[]", font1_ptr_element_enum_ptrs,
  354.   font1_ptr_element_reloc_ptrs, st_gs_font_type1_ptr);
  355. private int
  356. zbuildfont9(i_ctx_t *i_ctx_p)
  357. {
  358.     os_ptr op = osp;
  359.     build_proc_refs build;
  360.     int code = build_proc_name_refs(&build, NULL, "%Type9BuildGlyph");
  361.     gs_font_cid_data common;
  362.     ref GlyphDirectory, GlyphData, DataSource;
  363.     ref *prfda;
  364.     gs_font_type1 **FDArray;
  365.     uint FDArray_size;
  366.     int FDBytes;
  367.     uint CIDMapOffset;
  368.     gs_font_base *pfont;
  369.     gs_font_cid0 *pfcid;
  370.     uint i;
  371.  
  372.     /*
  373.      * If the CIDFont's data have been loaded into VM, GlyphData will be
  374.      * a string or an array of strings; if they are loaded incrementally
  375.      * from a file, GlyphData will be an integer, and DataSource will be
  376.      * a (reusable) stream.
  377.      */
  378.     if (code < 0 ||
  379.     (code = cid_font_data_param(op, &common, &GlyphDirectory)) < 0 ||
  380.     (code = dict_find_string(op, "FDArray", &prfda)) < 0 ||
  381.     (code = dict_int_param(op, "FDBytes", 0, MAX_FDBytes, -1, &FDBytes)) < 0
  382.     )
  383.     return code;
  384.     if (r_has_type(&GlyphDirectory, t_null)) {
  385.     /* Standard CIDFont, require GlyphData and CIDMapOffset. */
  386.     ref *pGlyphData;
  387.  
  388.     if ((code = dict_find_string(op, "GlyphData", &pGlyphData)) < 0 ||
  389.         (code = dict_uint_param(op, "CIDMapOffset", 0, max_uint - 1,
  390.                     max_uint, &CIDMapOffset)) < 0)
  391.         return code;
  392.     GlyphData = *pGlyphData;
  393.     if (r_has_type(&GlyphData, t_integer)) {
  394.         ref *pds;
  395.         stream *ignore_s;
  396.  
  397.         if ((code = dict_find_string(op, "DataSource", &pds)) < 0)
  398.         return code;
  399.         check_read_file(ignore_s, pds);
  400.         DataSource = *pds;
  401.     } else {
  402.         if (!r_has_type(&GlyphData, t_string) && !r_is_array(&GlyphData))
  403.         return_error(e_typecheck);
  404.         make_null(&DataSource);
  405.     }
  406.     } else {
  407.     make_null(&GlyphData);
  408.     make_null(&DataSource);
  409.     CIDMapOffset = 0;
  410.     }
  411.     if (!r_is_array(prfda))
  412.     return_error(e_invalidfont);
  413.     FDArray_size = r_size(prfda);
  414.     if (FDArray_size == 0)
  415.     return_error(e_invalidfont);
  416.     FDArray = ialloc_struct_array(FDArray_size, gs_font_type1 *,
  417.                   &st_gs_font_type1_ptr_element,
  418.                   "buildfont9(FDarray)");
  419.     if (FDArray == 0)
  420.     return_error(e_VMerror);
  421.     memset(FDArray, 0, sizeof(gs_font_type1 *) * FDArray_size);
  422.     for (i = 0; i < FDArray_size; ++i) {
  423.     ref rfd;
  424.  
  425.     array_get(prfda, (long)i, &rfd);
  426.     code = fd_array_element(i_ctx_p, &FDArray[i], &rfd);
  427.     if (code < 0)
  428.         goto fail;
  429.     }
  430.     code = build_gs_simple_font(i_ctx_p, op, &pfont, ft_CID_encrypted,
  431.                 &st_gs_font_cid0, &build,
  432.                 bf_Encoding_optional |
  433.                 bf_FontBBox_required |
  434.                 bf_UniqueID_ignored);
  435.     if (code < 0)
  436.     goto fail;
  437.     pfont->procs.enumerate_glyph = gs_font_cid0_enumerate_glyph;
  438.     pfont->procs.glyph_outline = z9_glyph_outline;
  439.     pfcid = (gs_font_cid0 *)pfont;
  440.     pfcid->cidata.common = common;
  441.     pfcid->cidata.CIDMapOffset = CIDMapOffset;
  442.     pfcid->cidata.FDArray = FDArray;
  443.     pfcid->cidata.FDArray_size = FDArray_size;
  444.     pfcid->cidata.FDBytes = FDBytes;
  445.     pfcid->cidata.glyph_data = z9_glyph_data;
  446.     pfcid->cidata.proc_data = 0;    /* for GC */
  447.     ref_assign(&pfont_data(pfont)->u.cid0.GlyphDirectory, &GlyphDirectory);
  448.     ref_assign(&pfont_data(pfont)->u.cid0.GlyphData, &GlyphData);
  449.     ref_assign(&pfont_data(pfont)->u.cid0.DataSource, &DataSource);
  450.     code = define_gs_font((gs_font *)pfont);
  451.     if (code >= 0)
  452.     return code;
  453.  fail:
  454.     ifree_object(FDArray, "buildfont9(FDarray)");
  455.     return code;
  456. }
  457.  
  458. /* <cid9font> <cid> .type9mapcid <charstring> <font_index> */
  459. private int
  460. ztype9mapcid(i_ctx_t *i_ctx_p)
  461. {
  462.     os_ptr op = osp;
  463.     gs_font *pfont;
  464.     gs_font_cid0 *pfcid;
  465.     int code = font_param(op - 1, &pfont);
  466.     gs_const_string gstr;
  467.     int fidx;
  468.  
  469.     if (code < 0)
  470.     return code;
  471.     if (pfont->FontType != ft_CID_encrypted)
  472.     return_error(e_invalidfont);
  473.     check_type(*op, t_integer);
  474.     pfcid = (gs_font_cid0 *)pfont;
  475.     code = pfcid->cidata.glyph_data((gs_font_base *)pfcid,
  476.             (gs_glyph)(gs_min_cid_glyph + op->value.intval),
  477.                     &gstr, &fidx);
  478.     if (code < 0)
  479.     return code;
  480.     make_const_string(op - 1, a_readonly, gstr.size, gstr.data);
  481.     make_int(op, fidx);
  482.     return 0;
  483. }
  484.  
  485. #if defined(DEBUG) || defined(PROFILE)
  486.  
  487. #include "gdevpsf.h"
  488.  
  489. /* <file> <cid9font> .writefont9 - */
  490. private int
  491. zwritefont9(i_ctx_t *i_ctx_p)
  492. {
  493.     os_ptr op = osp;
  494.     gs_font *pfont;
  495.     gs_font_cid0 *pfcid;
  496.     int code = font_param(op, &pfont);
  497.     stream *s;
  498.  
  499.     if (code < 0)
  500.     return code;
  501.     if (pfont->FontType != ft_CID_encrypted)
  502.     return_error(e_invalidfont);
  503.     check_write_file(s, op - 1);
  504.     pfcid = (gs_font_cid0 *)pfont;
  505.     code = psf_write_cid0_font(s, pfcid,
  506.                    WRITE_TYPE2_NO_LENIV | WRITE_TYPE2_CHARSTRINGS,
  507.                    NULL, 0, NULL);
  508.     if (code >= 0)
  509.     pop(2);
  510.     return code;
  511. }
  512.  
  513. #endif
  514.  
  515. /* ------ Initialization procedure ------ */
  516.  
  517. const op_def zfcid0_op_defs[] =
  518. {
  519.     {"2.buildfont9", zbuildfont9},
  520.     {"2.type9mapcid", ztype9mapcid},
  521. #if defined(DEBUG) || defined(PROFILE)
  522.     {"2.writefont9", zwritefont9},
  523. #endif
  524.     op_def_end(0)
  525. };
  526.